home *** CD-ROM | disk | FTP | other *** search
/ The Essential Home & Business Collection / The Essential Home & Business Collection.iso / 27 / 3 / 5 / HP22D5.ZIP / EXTERN / TSR.C < prev    next >
Text File  |  1991-04-16  |  4KB  |  180 lines

  1. /*
  2. ** TSR.C
  3. **
  4. ** This program is a sample TSR that registers itself with HyperPAD. When
  5. ** invoked from the DOS prompt, the TSR will unload itself. When invoked from
  6. ** within HyperPAD, the TSR will register a callback routine and then exit
  7. ** normally. Then, when all pending handlers have executed, HyperPAD will call
  8. ** the registered routine.
  9. **
  10. ** The trigger keys are LSHIFT+RSHIFT.
  11. **
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <dos.h>
  17.  
  18. #include "extern.h"
  19. #include "resident.h"
  20.  
  21. int (pascal far *callback)(int (_loadds *)());
  22. int (_interrupt *old9)();    /* previous interrupt 9 address */
  23.  
  24. /*
  25. ** comeup()
  26. **
  27. ** This routine is the TSR callback. It will be invoked after all pending
  28. ** handlers have executed. The address of this routine is passed to the
  29. ** registration routine.
  30. **
  31. ** The routines called from within this function are documented in the
  32. ** PADtalk Reference Guide in Appendix 3 - Writing Extensions. They are
  33. ** actually implemented within HyperPAD itself.
  34. **
  35. */
  36. _loadds comeup()
  37.  
  38. {
  39.     SHAREDPTR pShared;
  40.     MSG Msg;
  41.     PACKETPTR Screen;
  42.  
  43.     pShared = GetSharedArea();
  44.     Screen = pShared -> pVideoModes[0];
  45.  
  46.     HideMouse();
  47.     MakeWin(10,10,70,20,78,Screen);
  48.     PutStr(12,12,"Hello World",Screen);
  49.     ShowMouse();
  50.  
  51.     do {
  52.         GetEvent(&Msg);
  53.     } while (Msg.Event != EVENT_DOUBLECLICK);
  54.  
  55.     HideMouse();
  56.     RemWin();
  57.     ShowMouse();
  58.  
  59.     /* don't call this routine anymore */
  60.     (*callback)(NULL);
  61. }
  62.  
  63. /*
  64. ** _int9()
  65. **
  66. ** This routine gets called when a key is pressed, released, or repeated. It
  67. ** keeps track of the left and right shift keys. When both are pressed, 
  68. ** HyperPAD is notified and passed the address of the routine to call.
  69. **
  70. */
  71. _interrupt int9()
  72.  
  73. {
  74.     BYTE key;
  75.     static int lshift = 0;
  76.     static int rshift = 0;
  77.  
  78.     key = inp(0x60);
  79.  
  80.     if (!(key & 0x80)) {
  81.         /* a key is pressed */
  82.  
  83.         /* check to see if the key is either the left or right shift key */
  84.         if (key == 42) lshift = 1;
  85.         else if (key == 54) rshift = 1;
  86.  
  87.         if (lshift && rshift) {
  88.         /* both shift keys are pressed, tell HyperPAD about it */
  89.         if (callback = hploaded()) {
  90.             /* HyperPAD was installed, so pass it my routine */
  91.             (*callback)(comeup);
  92.         }
  93.         else unload();    /* HyperPAD wasn't there, unload the TSR */
  94.         }
  95.  
  96.     }
  97.     else {
  98.         /* a key was released */
  99.  
  100.         key &= 0x7f;
  101.         if (key == 42) lshift = 0;        /* left shift */
  102.         else if (key == 54) rshift = 0;    /* right shift */
  103.  
  104.     }
  105.  
  106.     /* chain to the previous interrupt 9 handler */
  107.     (*old9)();
  108. }
  109.  
  110. /*
  111. ** take9()
  112. **
  113. ** This routine chains into interrupt 9 - the keyboard interrupt.
  114. **
  115. */
  116. take9()
  117.  
  118. {
  119.     old9 = _dos_getvect(0x09);
  120.     _dos_setvect(0x09,int9);
  121. }
  122.  
  123. /*
  124. ** return9()
  125. **
  126. ** This routine removes the TSR from the interrupt 9 chain.
  127. **
  128. */
  129. return9()
  130.  
  131. {
  132.     _dos_setvect(0x09,old9);
  133. }
  134.  
  135. /*
  136. ** reside()
  137. **
  138. ** This routine performs the necessary steps to leave me resident.
  139. **
  140. */
  141. reside()
  142.  
  143. {
  144.     WORD *p;
  145.     WORD *EndBlock;
  146.  
  147.     take9();
  148.  
  149.     /* free the environment */
  150.     FP_SEG(p) = _psp;
  151.     FP_OFF(p) = 0x2c;
  152.     _dos_freemem(*p);
  153.  
  154.     /* find the end of program pointer */
  155.     FP_SEG(EndBlock) = _psp;    /* find psp segment */
  156.     FP_OFF(EndBlock) = 2;        /* set end of program pointer */
  157.  
  158.     /* terminate and stay resident */
  159.     _dos_keep(0,*EndBlock - _psp);
  160. }
  161.  
  162. /*
  163. ** unload()
  164. **
  165. ** This routine removes the TSR from memory.
  166. **
  167. */
  168. unload()
  169.  
  170. {
  171.     return9();
  172.     _dos_freemem(_psp);
  173. }
  174.  
  175. main()
  176.  
  177. {
  178.     reside();
  179. }
  180.